package com.mkvld.appietijdenv2.databases;

import java.util.ArrayList;
import java.util.List;

import com.mkvld.appietijdenv2.data.Holiday;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class HolidayDatabase extends SQLiteOpenHelper {
	private static final String DB_NAME = "holidays";
	private static final int DB_VERSION = 8;
	
	private static final String TABLE_NAME = "holidays";
	private static final String KEY_YEAR = "year";
	private static final String KEY_WEEK = "week";
	private static final String KEY_DAY = "day";
	private static final String KEY_NAME = "name";
	private static final String[] KEYS = { KEY_YEAR, KEY_WEEK, KEY_DAY, KEY_NAME };
	
	private static final String CREATE_STRING = "CREATE TABLE " + TABLE_NAME
				+ " (" + KEY_YEAR + " INTEGER, " 
			+ KEY_WEEK + " INTEGER, " 
			+ KEY_DAY + " INTEGER, " 
			+ KEY_NAME + " TEXT)";
	
	public HolidayDatabase(Context context) {
		super(context, DB_NAME, null, DB_VERSION);
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_STRING);
		addHoliday(db, 2013, 0, 1, "Nieuwjaarsdag");
		addHoliday(db, 2013, 12, 6, "1e Paasdag");
		addHoliday(db, 2013, 13, 1, "2e Paasdag");
		addHoliday(db, 2013, 17, 1, "Koninginnedag");
		addHoliday(db, 2013, 18, 3, "Hemelvaartsdag");
		addHoliday(db, 2013, 19, 6, "1e Pinksterdag");
		addHoliday(db, 2013, 20, 1, "2e Pinksterdag");
		addHoliday(db, 2013, 51, 2, "1e kerstdag");
		addHoliday(db, 2013, 51, 3, "2e kerstdag");
		
		addHoliday(db, 2014, 0, 2, "Nieuwjaarsdag");
		addHoliday(db, 2014, 15, 6, "1e Paasdag");
		addHoliday(db, 2014, 16, 1, "2e Paasdag");
		addHoliday(db, 2014, 17, 5, "Koningsdag");
		addHoliday(db, 2014, 21, 3, "Hemelvaartsdag");
		addHoliday(db, 2014, 22, 6, "1e Pinksterdag");
		addHoliday(db, 2014, 23, 1, "2e Pinksterdag");
		addHoliday(db, 2014, 51, 3, "1e kerstdag");
		addHoliday(db, 2014, 51, 4, "2e kerstdag");
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
		onCreate(db);
	}
	
	private Holiday cursorToHoliday(Cursor cursor) {
		int year = Integer.parseInt(cursor.getString(0));
		int week = Integer.parseInt(cursor.getString(1));
		int day = Integer.parseInt(cursor.getString(2));
		String name = cursor.getString(3);
		return new Holiday(year, week, day, name);
	}
	
	private void addHoliday(SQLiteDatabase db, int year, int week, int day, String name) {
		ContentValues values = new ContentValues();
		values.put(KEY_YEAR, year);
		values.put(KEY_WEEK, week);
		values.put(KEY_DAY, day);
		values.put(KEY_NAME, name);
		db.insert(TABLE_NAME, null, values);
	}
	
	public List<Holiday> getHolidays(int year, int period) {
		SQLiteDatabase db = this.getReadableDatabase();
		String selection = KEY_YEAR + " = ? AND " + KEY_WEEK + " >= ? AND " + KEY_WEEK + " < ?";
		String selectionArgs[] = {
			Integer.toString(year),
			Integer.toString(period * 4),
			Integer.toString((period + 1) * 4)
		};
		List<Holiday> holidays = new ArrayList<Holiday>();
		Cursor cursor = db.query(TABLE_NAME, KEYS, selection, selectionArgs, null, null, null);
		if (cursor.moveToFirst()) {
			do {
				holidays.add(cursorToHoliday(cursor));
			} while (cursor.moveToNext());
		}
		return holidays;
	}
	
}
